$(document).ready(function(){
    var eDonationBtn = $('#donationBtn'), eModal = $('#modal'), eModalBtn = $('#modalOk'), eGxInput = $('#gxInput');
    eDonationBtn.on('click', function(e){
        e.preventDefault();
        eModal.removeClass('hide').addClass('modal-show');
        window.setTimeout(function(){
            $('.modal').addClass('in');
        },20);
        eGxInput.focus();
    });
    eGxInput.blur(function(){
        var gx = eGxInput.val();
        var maxGx = eGxInput.data('gx');
        if(gx > 0 && gx <= maxGx) $(this).removeClass('error');
    });
    // 关闭modal
    eModal.on('click', '#modalClose,#modalNo', function(){
        eModal.removeClass('modal-show').removeClass('in');
        window.setTimeout(function(){
            eModal.addClass('hide');
        }, 1000);
    });
    eModalBtn.on('click', function(e){
        e.preventDefault();
        var gx = eGxInput.val();
        var maxGx = eGxInput.data('gx');
        if(gx <= 0 || gx > maxGx){
            eGxInput.addClass('error');
            return ;
        }
        $.post(eModalBtn.data('url'), {gx: gx}, function(res){
            var data = $.parseJSON(res);
            if(data.code == 200){
                var newGx = maxGx - gx;
                eGxInput.data('gx', newGx).val('');
                $('#userAllGx').text(newGx);
                $('#modalClose').click();
            }
            clearpop(data.msg);
        });
    });
});